package com.puhui.education.fragment.main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.puhui.education.R;
import com.puhui.education.activity.LearnCardAcy;
import com.puhui.education.activity.MyCollectionAcy;
import com.puhui.education.activity.MyCourseAcy;
import com.puhui.education.activity.MyOrderFormAcy;
import com.puhui.education.activity.PersonalInfoAcy;
import com.puhui.education.activity.RechargeCardAcy;
import com.puhui.education.activity.SettingsAcy;
import com.puhui.education.adapter.ViewHolder;
import com.puhui.education.adapter.base.recyclerview.MultiItemCommonAdapter;
import com.puhui.education.adapter.base.recyclerview.MultiItemTypeSupport;
import com.puhui.education.bean.CourseStatus;
import com.puhui.education.bean.HorizontalBarInfo;
import com.puhui.education.bean.MineFragmentRecyclerViewData;
import com.puhui.education.fragment.BaseFrag;
import com.puhui.education.util.DynamicLayoutParamsUtil;
import com.puhui.education.util.IntentUtil;
import com.puhui.education.view.CommonToolBar;
import com.puhui.education.view.MessageView;
import com.puhui.education.view.TextViewPlus;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/5/28.
 */
public class MineFrag extends BaseFrag {
    @Bind(R.id.cb_title_mineFrag)
    CommonToolBar cv_title;
    @Bind(R.id.rv_mineFrag)
    RecyclerView rv;
    @Bind(R.id.mv_learning_mineFrag)
    MessageView mv_learning;
    @Bind(R.id.mv_notLearn_mineFrag)
    MessageView mv_notLearn;
    @Bind(R.id.mv_learnComplete_mineFrag)
    MessageView mv_learnComplete;
    @Bind(R.id.mv_expiration_mineFrag)
    MessageView mv_expiration;
    private Activity activity;
    private View view;
    private List<MineFragmentRecyclerViewData> recyclerViewData;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.frag_mine, null);
        }
        ButterKnife.bind(this, view);
        initData();
        initEvent();
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (Activity) context;
    }
@OnClick(R.id.tv_accountInfo_mineFrag)
public void onAccountInfoClick() {
    IntentUtil.startActivity(activity, PersonalInfoAcy.class);
}

    @OnClick(R.id.prl_myCourse_mineFrag)
    public void onMyCourseClick() {
        Intent intent = new Intent(activity, MyCourseAcy.class);
        intent.putExtra("Page", CourseStatus.ALL);
        startActivity(intent);
        activity.overridePendingTransition(R.anim.activity_enter_anim, R.anim.activity_exit_anim);
    }

    @OnClick({R.id.mv_learning_mineFrag, R.id.mv_notLearn_mineFrag, R.id.mv_learnComplete_mineFrag, R.id.mv_expiration_mineFrag})
    public void onLearnStatusItemClick(View view) {
        Intent intent = new Intent(activity, MyCourseAcy.class);
        switch (view.getId()) {
            case R.id.mv_learning_mineFrag:
                intent.putExtra("Page", CourseStatus.LEARNING);
                mv_learning.setEmptyMessage();
                break;
            case R.id.mv_notLearn_mineFrag:
                intent.putExtra("Page", CourseStatus.NOT_LEARN);
                break;
            case R.id.mv_learnComplete_mineFrag:
                intent.putExtra("Page", CourseStatus.COMPLETE_LEARN);
                break;
            case R.id.mv_expiration_mineFrag:
                intent.putExtra("Page", CourseStatus.EXPIRATION);
                break;
        }
        startActivity(intent);
        activity.overridePendingTransition(R.anim.activity_enter_anim, R.anim.activity_exit_anim);

    }

    @Override
    protected void initData() {
        super.initData();
        initRecyclerViewData();
        rv.setLayoutManager(new LinearLayoutManager(activity,LinearLayoutManager.VERTICAL,false));
        rv.setAdapter(new MineFragRecyclerViewAdapter(activity, recyclerViewData));
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        cv_title.setLeftImgOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtil.startActivity(activity, SettingsAcy.class);
            }
        });
    }

    private void initRecyclerViewData() {
        recyclerViewData = new ArrayList<>();
        recyclerViewData.add(new MineFragmentRecyclerViewData(MineFragmentRecyclerViewData.HORIZONTAL_DIVIDER, null));
        recyclerViewData.add(new MineFragmentRecyclerViewData(MineFragmentRecyclerViewData.INFO_BAR,
                new HorizontalBarInfo(R.mipmap.home_selected, "收藏课程")));
        recyclerViewData.add(new MineFragmentRecyclerViewData(MineFragmentRecyclerViewData.HORIZONTAL_VIEW, null));
        recyclerViewData.add(new MineFragmentRecyclerViewData(MineFragmentRecyclerViewData.INFO_BAR,
                new HorizontalBarInfo(R.mipmap.course_selected, "我的订单")));
        recyclerViewData.add(new MineFragmentRecyclerViewData(MineFragmentRecyclerViewData.HORIZONTAL_DIVIDER, null));
        recyclerViewData.add(new MineFragmentRecyclerViewData(MineFragmentRecyclerViewData.INFO_BAR,
                new HorizontalBarInfo(R.mipmap.cart_selected, "充值卡")));
        recyclerViewData.add(new MineFragmentRecyclerViewData(MineFragmentRecyclerViewData.HORIZONTAL_VIEW, null));
        recyclerViewData.add(new MineFragmentRecyclerViewData(MineFragmentRecyclerViewData.INFO_BAR,
                new HorizontalBarInfo(R.mipmap.mine_selected, "学习卡")));
        recyclerViewData.add(new MineFragmentRecyclerViewData(MineFragmentRecyclerViewData.HORIZONTAL_DIVIDER, null));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}

class MineFragRecyclerViewAdapter extends MultiItemCommonAdapter<MineFragmentRecyclerViewData> {
    private Activity activity;

    public MineFragRecyclerViewAdapter(Context context, List<MineFragmentRecyclerViewData> datas) {
        super(context, datas, new MultiItemTypeSupport<MineFragmentRecyclerViewData>() {
            @Override
            public int getLayoutId(int itemType) {
                switch (itemType) {
                    case MineFragmentRecyclerViewData.HORIZONTAL_DIVIDER:
                        return R.layout.item_rv_horizontal_divider;
                    case MineFragmentRecyclerViewData.INFO_BAR:
                        return R.layout.item_rv_horizontal_bar_mine_frag;
                    case MineFragmentRecyclerViewData.HORIZONTAL_VIEW:
                        return R.layout.item_rv_horizontal_view;
                    default:
                        return -1;
                }
            }

            @Override
            public int getItemViewType(int position, MineFragmentRecyclerViewData mineFragmentRecyclerViewData) {
                return mineFragmentRecyclerViewData.getLayoutType();
            }
        });
        activity= (Activity) context;
    }

    @Override
    public void convert(final ViewHolder holder, MineFragmentRecyclerViewData mineFragmentRecyclerViewData) {
        if (holder.getLayoutId() == R.layout.item_rv_horizontal_divider) {
            holder.getView(R.id.v_divider_rvItem).setLayoutParams(DynamicLayoutParamsUtil.createViewGroupLayoutParams(DynamicLayoutParamsUtil.WIDTH_MATCH, -1f, 0.018f));
        } else if (holder.getLayoutId() == R.layout.item_rv_horizontal_bar_mine_frag) {
            holder.getView(R.id.prl_horizontalInfoBar_rvItem).setLayoutParams(DynamicLayoutParamsUtil.createViewGroupLayoutParams(DynamicLayoutParamsUtil.WIDTH_MATCH, -1f, 0.085f));
            TextViewPlus textViewPlus = holder.getView(R.id.tvp_title_rvItem);
            textViewPlus.setCompoundImg(TextViewPlus.LEFT_IMG, mineFragmentRecyclerViewData.getHorizontalBarInfo().getImgResourceId(), 0.035f, 0.035f,"ScreenHeight");
            textViewPlus.setText(mineFragmentRecyclerViewData.getHorizontalBarInfo().getTitle());
            holder.setOnClickListener(R.id.prl_horizontalInfoBar_rvItem, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (holder.getAdapterPosition()) {
                        case 1:
                            IntentUtil.startActivity(activity,MyCollectionAcy.class);
                            break;
                        case 3:
                            IntentUtil.startActivity(activity,MyOrderFormAcy.class);
                            break;
                        case 5:
                            IntentUtil.startActivity(activity,RechargeCardAcy.class);
                            break;
                        case 7:
                            IntentUtil.startActivity(activity,LearnCardAcy.class);
                            break;
                    }
                }
            });

        }
    }
}